package cn.gjculture.shopping.api.controller;

import cn.gjculture.shopping.api.entity.*;
import cn.gjculture.shopping.api.service.SupplierService;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/supplier")
public class SupplierController {

    @Autowired
    SupplierService supplierService;


    @ApiOperation(value = "新增供应商", notes = "新增供应商")
    @ApiImplicitParam(name = "supplier", value = "供应商对象", required = true, dataType = "Object")
    @PostMapping
    public Return<Supplier> save(String param) {
        ParamData<Supplier> g = JSONObject.parseObject(param, new TypeReference<ParamData<Supplier>>() {});
        Return ret = new Return();
        if (supplierService.insert(g.getData())>0) {
            ret.getCommon().setRet(true);
        }
        return ret;
    }

    @ApiOperation(value="修改供应商", notes="修改供应商")
    @ApiImplicitParam(name = "Supplier", value = "供应商对象", required = true, dataType = "Object")
    @PutMapping
    public Return update(String param){
        ParamData<Supplier> g = JSONObject.parseObject(param, new TypeReference<ParamData<Supplier>>() {});
        Return ret=new Return();
        if( supplierService.update(g.getData())>0){
            ret.getCommon().setRet(true);
        }
        return ret;
    }

    @ApiOperation(value = "获取供应商列表", notes = "获取供应商列表")
    @GetMapping
    public Return<List<Supplier>> query(PageInfo pageInfo) {
        Return<List<Supplier>> ret = new Return<>();
        List<Supplier> suppliers = supplierService.query();
        ret.setData(suppliers);
        return ret;
    }
    @ApiOperation(value = "获取供应商列表", notes = "获取供应商列表")
    @GetMapping(value = "/forRoutingRule")
    public Return<List<Supplier>> queryForRoutingRule(PageInfo pageInfo) {
        Return<List<Supplier>> ret = new Return<>();
        List<Supplier> suppliers = supplierService.queryForRoutingRule();
        ret.setData(suppliers);
        return ret;
    }

    @ApiOperation(value = "获取供应商", notes = "获取供应商")
    @ApiImplicitParam(name = "supplierId", value = "供应商ID", required = true, dataType = "String",paramType = "path")
    @GetMapping(value = "/{supplierId}")
    public Return<Supplier> getHotelById(@PathVariable(value = "supplierId") String supplierId) {
        Return<Supplier> ret = new Return<>();
        Supplier supplier = supplierService.queryById(supplierId);
        ret.setData(supplier);
        return ret;
    }


    @ApiOperation(value = "删除供应商", notes = "删除供应商")
    @ApiImplicitParam(name = "supplierIds", value = "供应商ID", required = true,  dataType = "String",paramType = "path")
    @DeleteMapping(value = "/{supplierIds}")
    public Return<Supplier> delete(@PathVariable(value = "supplierIds") List<String> supplierIds) {
        Return<Supplier> ret = new Return<>();
        if(supplierService.delete(supplierIds)>0){
            ret.getCommon().setRet(true);
        }
        return ret;
    }
}
